package com.zbxx.springboot1.Action;


import com.zbxx.springboot1.domain.User;
import com.zbxx.springboot1.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class UserAction {
    @Resource
    private UserService userService;
    //登录验证
    @RequestMapping("/login")//
    public String login(String name, String password, HttpSession session, Model model) throws Exception {
        if (name == null)
            return "login";
            User user = userService.login(name, password);
             System.out.println(user.toString());
            if (user != null && password.equals(user.getPassword())) {
                session.setAttribute("user", name);
                return "list";
            } else
                return "error";

    }
    //查询所有
    @RequestMapping("/SelectUserAll")
    public ModelAndView SelectUserAll(User user, Model model) {
        List<User> list = userService.SelectUserAll();
        ModelAndView mw = new ModelAndView();
        mw.addObject("list", list);
        System.out.println("list====" + list);
        mw.setViewName("getuser");
        return mw;
    }

    //删除
    @RequestMapping("/deleteUser")
    public String deleteUser(Long id, Model model) {
        int num = userService.deleteUser(id);
        if ("".equals(num)) {
            model.addAttribute("error", "删除失败");
            return "error";//返回错误界面
        } else {
            return "redirect:SelectUserAll";//删除成功以后，返回剩下所有数据的界面
        }
    }

    //查询修改用户
    @RequestMapping("/editUserId")
    public String editUserId(Long id, Model model) {
        User user = userService.editUserId(id);
        model.addAttribute("user", user);
        return "edit";
    }

    //修改用户
    @RequestMapping("/editUser")
    public String editUser(User user, Model model) {
        int num = userService.editUser(user);
        if (num > 0) {
            return "redirect:SelectUserAll";//编辑成功返回查询所有人界面
        } else {
            model.addAttribute("error", "编辑失败");
            return "error";
        }
    }

    //添加用户
    @RequestMapping("/addUser")
    public String addUser(User user, Model model) {
        if (user.getName()==null)
            return "addUser";
        int num = userService.addUser(user);
        System.out.println(num);
        if (num >0){
//            int num = userService.addUser(user);
            return "redirect:SelectUserAll";
            } else{
            model.addAttribute("error", "添加失败");
            return "error";
        }
    }


    //注册
    @RequestMapping("/reg")
    public String reg(User user, Model model) {
        if(user.getName()==null) {
//            System.out.println(user);
            return "reg";
        }
        int num = userService.reg(user);
        if (num > 0) {
            return "login";
        } else {
            model.addAttribute("user", user);
            return "error";
        }
    }
    //注销
    @RequestMapping("/logon")
    public String logon(HttpServletRequest request) {
        HttpSession session = request.getSession();
        System.out.println("333" + session);
        session.invalidate();
        return "login";
    }

    //搜索
    @RequestMapping("/find")
    public String find(User user, Model model) {
        List<User> list2 = userService.findinfo(user);
        if ("".equals(list2)) {
            model.addAttribute("error", "请输入正确的用户名");
            return "error";
        } else {
            model.addAttribute("list", list2);
            return "getuser";
        }
    }
}

